import numpy as np
import cv2

# 加载图片 读取彩色图像归一化且转换为浮点型
image = cv2.imread("D:\\download\\450d23eaec784b8d19f413014b020f3b.jpeg", cv2.IMREAD_COLOR).astype(np.float32) / 255.0

# 颜色空间转换 BGR转为HLS
hlsImg = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)

# 调节饱和度和亮度的窗口
cv2.namedWindow("saturation")

# 创建滑动块
cv2.createTrackbar("saturation", "saturation", 0, 100, lambda x: x)

# 调整饱和度和亮度
while True:
    # 复制原图
    hlsCopy = np.copy(hlsImg)
    # 得到 saturation 的值
    saturation = cv2.getTrackbarPos('saturation', 'saturation')
    # 饱和度
    hlsCopy[:, :, 2] = (1.0 + saturation / float(100)) * hlsCopy[:, :, 2]
    hlsCopy[:, :, 2][hlsCopy[:, :, 2] > 1] = 1
    # HLS2BGR
    newImg = cv2.cvtColor(hlsCopy, cv2.COLOR_HLS2BGR)
    # 显示调整后的效果
    cv2.imshow("saturation", newImg)
    ch = cv2.waitKey(5)
    # 按 ESC 键退出
    if ch == 27:
        break

# 关闭所有的窗口
cv2.destroyAllWindows()
